package com.cskaoyan.market.controller;


import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.service.AdminBrandService;
import com.cskaoyan.market.service.impl.AdminBrandServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;



@MultipartConfig
@WebServlet("/admin/brand/*")
public class AdminBrandController extends HttpServlet {
    AdminBrandService adminBrandService =new AdminBrandServiceImpl();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op =requestURI.replace(req.getContextPath()+"/admin/brand/","");
        if("create".equals(op)){
            create(req,resp);
        }
        if("update".equals(op)){
            update(req,resp);
        }
        if("delete".equals(op)){
            delete(req,resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String requestURI = req.getRequestURI();
       String op =requestURI.replace(req.getContextPath()+"/admin/brand/","");
       if("list".equals(op)){
           list(req,resp);
       }



    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String marketBrandJson = req.getReader().readLine();
        MarketBrand marketBrand = objectMapper.readValue(marketBrandJson, MarketBrand.class);
        if(adminBrandService.delete(marketBrand)==1){
            Object ok = ResponseUtil.ok();
            String s = JacksonUtil.writeValueAsString(ok);
            resp.getWriter().write(s);
            return ;
        }
        Object fail = ResponseUtil.badDelete();
        String respJson = JacksonUtil.getObjectMapper().writeValueAsString(fail);
        resp.getWriter().write(respJson);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String marketBrandJson = req.getReader().readLine();

        MarketBrand marketBrand=objectMapper.readValue(marketBrandJson,MarketBrand.class);

        marketBrand.setUpdateTime(LocalDateTime.now());

        if(adminBrandService.update(marketBrand)==1){
            Object ok = ResponseUtil.ok(marketBrand);
            String s = JacksonUtil.writeValueAsString(ok);
            resp.getWriter().write(s);
            return ;

        }
        Object fail = ResponseUtil.badUpdate();
        String respJson = JacksonUtil.getObjectMapper().writeValueAsString(fail);
        resp.getWriter().write(respJson);


    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = req.getReader().readLine();
        MarketBrand marketBrand = objectMapper.readValue(s, MarketBrand.class);

        marketBrand.setAddTime(LocalDateTime.now());
        marketBrand.setUpdateTime(LocalDateTime.now());
        if(adminBrandService.create(marketBrand)==1)
        {
            Object ok = ResponseUtil.ok(marketBrand);
            String respJson = JacksonUtil.getObjectMapper().writeValueAsString(ok);
            resp.getWriter().write(respJson);
            return;
        };
        Object fail = ResponseUtil.badCreate();
        String respJson = JacksonUtil.getObjectMapper().writeValueAsString(fail);
        resp.getWriter().write(respJson);

    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page=req.getParameter("page");
        String limit=req.getParameter("limit");
        String sort=req.getParameter("sort");
        String order=req.getParameter("order");
        String id = req.getParameter("id");
        String name = req.getParameter("name");


        List<MarketBrand> brandList= adminBrandService.list(page,limit,sort,order,id,name);
        Object o = ResponseUtil.okList(brandList);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().println(s);

    }
}
